package com.nulldev.util.threading.legacy.dbg;

import java.lang.management.ThreadInfo;
import java.util.Comparator;

public class ThreadInfoStats extends AbstractInfoStats{
    static int historyLength;
    private ThreadInfo threadInfo;
    private long threadId;
    private long previousCpuTime;
    private int updateCount;
    private boolean showTrace;
    private int index;
    private Integer threadPriority;

    public ThreadInfoStats(final long threadId, final ThreadInfo threadInfo, final long previousCpuTime) {
        this.threadId = threadId;
        this.threadInfo = threadInfo;
        this.previousCpuTime = previousCpuTime;
        this.usageHistory = new int[ThreadInfoStats.historyLength];
    }

    public String getName() {
        return this.threadInfo.getThreadName();
    }

    public long getId() {
        return this.threadId;
    }

    public boolean getSelect() {
        return this.showTrace;
    }

    public void setSelect(final boolean showTrace) {
        this.showTrace = showTrace;
    }

    public Thread.State getState() {
        return this.threadInfo.getThreadState();
    }

    public long update(final ThreadInfo threadInfo, final long previousCpuTime) {
        this.usageHistory[this.historyIndex] = this.percentage;
        this.historyIndex = ++this.historyIndex % ThreadInfoStats.historyLength;
        if (this.nrOfValidValues <= ThreadInfoStats.historyLength) {
            ++this.nrOfValidValues;
        }
        this.threadInfo = threadInfo;
        this.lastCpuUsage = previousCpuTime - this.previousCpuTime;
        if (this.lastCpuUsage < 0L) {
            this.lastCpuUsage = 0L;
        }
        this.previousCpuTime = previousCpuTime;
        ++this.updateCount;
        return this.lastCpuUsage;
    }

    public boolean checkUpdate(final int updateCount) {
        if (this.updateCount == updateCount) {
            return true;
        }
        assert this.updateCount == 0;
        this.lastCpuUsage = 0L;
        this.updateCount = updateCount;
        return false;
    }

    public void computePercentage(final long n) {
        if (n != 0L) {
            this.percentage = (int)(100L * this.lastCpuUsage / n);
        }
    }

    public String getHistoryAsString() {
        final StringBuffer sb = new StringBuffer();
        boolean b = false;
        for (int i = 0; i < ThreadInfoStats.historyLength; ++i) {
            final int n = this.usageHistory[(this.historyIndex + i) % ThreadInfoStats.historyLength];
            if (n > 0) {
                b = true;
            }
            sb.append(n);
            sb.append(" ");
        }
        if (b) {
            return sb.toString();
        }
        return "";
    }

    static Comparator<InfoStats> lastUsageComparator() {
        return new Comparator<InfoStats>() {
            public int compare(final InfoStats infoStats, final InfoStats infoStats2) {
                ThreadInfoStats threadInfoStats = null;
                ThreadInfoStats threadInfoStats2 = null;
                if (infoStats instanceof ThreadInfoStats) {
                    threadInfoStats = (ThreadInfoStats)infoStats;
                }
                if (infoStats2 instanceof ThreadInfoStats) {
                    threadInfoStats2 = (ThreadInfoStats)infoStats2;
                }
                if (threadInfoStats == null && threadInfoStats2 == null) {
                    return 0;
                }
                if (threadInfoStats == null) {
                    return -1;
                }
                if (threadInfoStats2 == null) {
                    return 1;
                }
                if (threadInfoStats.lastCpuUsage < threadInfoStats2.lastCpuUsage) {
                    return 1;
                }
                if (threadInfoStats.lastCpuUsage > threadInfoStats2.lastCpuUsage) {
                    return -1;
                }
                if (threadInfoStats.previousCpuTime < threadInfoStats2.previousCpuTime) {
                    return 1;
                }
                if (threadInfoStats.previousCpuTime > threadInfoStats2.previousCpuTime) {
                    return -1;
                }
                return 0;
            }
        };
    }

    static Comparator<ThreadInfoStats> nameComparator() {
        return new Comparator<ThreadInfoStats>() {
            public int compare(final ThreadInfoStats threadInfoStats, final ThreadInfoStats threadInfoStats2) {
                return threadInfoStats.getName().compareTo(threadInfoStats2.getName());
            }
        };
    }

    public static Comparator<InfoStats> fixOrderComparator() {
        return new Comparator<InfoStats>() {
            public int compare(final InfoStats infoStats, final InfoStats infoStats2) {
                ThreadInfoStats threadInfoStats = null;
                ThreadInfoStats threadInfoStats2 = null;
                if (infoStats instanceof ThreadInfoStats) {
                    threadInfoStats = (ThreadInfoStats)infoStats;
                }
                if (infoStats2 instanceof ThreadInfoStats) {
                    threadInfoStats2 = (ThreadInfoStats)infoStats2;
                }
                if (threadInfoStats == null && threadInfoStats2 == null) {
                    return 0;
                }
                if (threadInfoStats == null) {
                    return -1;
                }
                if (threadInfoStats2 == null) {
                    return 1;
                }
                if (threadInfoStats.index == 0 && threadInfoStats2.index == 0) {
                    return new Long(threadInfoStats.lastCpuUsage).compareTo(Long.valueOf(threadInfoStats2.lastCpuUsage));
                }
                if (threadInfoStats.index == 0) {
                    return 1;
                }
                if (threadInfoStats2.index == 0) {
                    return -1;
                }
                return new Integer(threadInfoStats.index).compareTo(Integer.valueOf(threadInfoStats2.index));
            }
        };
    }

    public void setIndex(final int index) {
        this.index = index;
    }

    public int getIndex() {
        return this.index;
    }

    public Integer getPriority() {
        return this.threadPriority;
    }

    public void setThreadPriority(final Integer threadPriority) {
        this.threadPriority = threadPriority;
    }

    static {
        ThreadInfoStats.historyLength = 10;
    }
}
